package com.flong.lambda;

import com.alibaba.fastjson.JSON;
import com.flong.vo.Person;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @Author:liangjl
 * @Date: 2020/6/20-11:07
 * @Eamil:jilongliang@sina.com
 * @Description: 1、lambda表达式就是对接口的抽象方法重写
 * 2、lambda表达式简化了内部类代码的冗余
 */
public class Test2 {


    @Test
    public void sort1() {

        List<Person> personList = dataList();
       /* //升序
        Collections.sort(personList, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getAge() - o2.getAge() ;
            }
        });*/

        //降序
        Collections.sort(personList, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o2.getAge() - o1.getAge() ;
            }
        });
        System.out.println(JSON.toJSON(personList));
       // dataList().forEach(o-> System.out.println(o.getAge()));
    }


    @Test
    public void sort2() {

        List<Person> personList = dataList();
        //升序
        personList.sort((o1, o2) -> o1.getAge()-o2.getAge());
        //降序
        personList.sort((o1, o2) -> o2.getAge()-o1.getAge());

        //personList.sort((Person o1, Person o2) -> o2.getAge()-o1.getAge());

        System.out.println(JSON.toJSON(personList));
    }


    private List<Person> dataList() {
        List<Person> personList = new ArrayList<>();
        Person p1 = Person.builder().username("周伯通").age(100).address("华山派").build();
        Person p2 = Person.builder().username("瑛姑").age(80).address("皇宫").build();
        Person p3 = Person.builder().username("一灯大师").age(118).address("少林寺").build();

        personList.add(p1);
        personList.add(p2);
        personList.add(p3);
        return personList;
    }

}
